<?php
Database($_SESSION['cid']);
header("content-type:text/html;charset=utf-8");
class LeaveAction extends Action{
	
	public function index(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$leave = D('Company/Leave')->more_data();
		//echo M('leave','','DB_MEETING')->getlastsql();
		foreach($leave as $k=>$v){
			$leave[$v['leave_id']] = $v;
			$hour = ($v['leave_endTime']-$v['leave_startTime'])%(24*3600);
			$leave[$v['leave_id']]['leave_days'] = $hour==0?($v['leave_endTime']-$v['leave_startTime'])/(24*3600)."天":floor(($v['leave_endTime']-$v['leave_startTime'])/(24*3600))."天/".floor($hour/3600)."小时";
		}
		$page = pageing('leave',7,'',$_GET['seach'],'leave_id desc','1','leave_uid','',"DB_MEETING");
		$this->assign("page",$page['data']);
		$this->assign("show",$page['show']);
		$this->assign('leave',$leave);
		$this->display();
	}
//请假搜索
	public function search(){
		if($_POST){
		$audience =D('Company/Leave')->audience_search($_POST['search']);
		
		foreach($audience as $key=>$value ){
			$leave_str.=$value['audience_id'].",";
		}
		
		$leave_audience['leave_uid'] = array("in",$leave_str);
		$time = strtotime($_POST['time']);	
		
		$leave_audience['leave_startTime'] = array("egt",$time);		
		
		$leave = M('leave','','DB_MEETING')->join('leave_type on leave.leave_type = leave_type.type_id')->join('leave_prove on leave.leave_id = leave_prove.leave_id')->join('audience on leave.leave_uid = audience.audience_id')->where($leave_audience)->field('leave.leave_id,leave.leave_uid,leave.leave_startTime,leave.leave_endTime,leave.leave_type,leave.leave_reason,leave.leave_state,leave_type.type_name,leave_type.type_prove,leave_prove.prove_image_path,audience.audience_name')->order('leave.leave_id desc')->select();	
		
		foreach($leave as $k=>$v){
			$leaves[$v['leave_id']] = $v;
			$hour = ($v['leave_endTime']-$v['leave_startTime'])%(24*3600);
			$leaves[$v['leave_id']]['leave_days'] = $hour==0?($v['leave_endTime']-$v['leave_startTime'])/(24*3600)."天":floor(($v['leave_endTime']-$v['leave_startTime'])/(24*3600))."天/".floor($hour/3600)."小时";
		}
		
		$count = count($leaves);
		$pagesize = 10;
		$totalPage =ceil($count/$pagesize);		
		$page = $_POST['page'];
		$leave_data = array_slice($leaves,($page-1)*$pagesize,$pagesize);
		
		if($page>1){
			$rs['show']="<li style='cursor:pointer'><a class='first' name='1' title='First Page'>&laquo; 首页</a></li>
		  <li style='cursor:pointer'><a class='prev' title='Previous Page'>&laquo; 上一页</a></li>";	
		}
		$offset = $pagesize*($page-1);
		$init = 1;
		$max = $totalPage;
		$pagelen=9;
		$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
		$pageoffset = ($pagelen-1)/2;
		
		if($totalPage>$pagelen){
			if($page<=$pageoffset){
				$init=1;
				$max = $pagelen;
			}else{
				if($page+$pageoffset>=$totalPage+1){
					$init = $totalPage-$pagelen+1;
				}else{
					$init = $page-$pageoffset;
					$max = $page+$pageoffset;
				}
			}
		}
		//生成html
		for($i=$init;$i<=$max;$i++){
			$current=$i==$page?'current':'';
			$rs['show'].="
				<li style='cursor:pointer'><a class='number {$current}' title='{$i}'>{$i}</a></li>
			";
		}
		if($page<$totalPage){
			$rs['show'].="<li style='cursor:pointer'><a class='next' title='Next Page'>下一页 &raquo;</a></li>
				  <li style='cursor:pointer'><a class='last' name='{$totalPage}' title='Last Page'>尾页 &raquo;</a></li>
			";
		}		

		$this->assign("page",$leave_data);
		$this->assign("show",$rs['show']);
		$this->assign('leave',$leaves);
		$this->display();
		}
		
		

	}
	
	//public function forAct(){
		//$sql_leave="SELECT 'leave.leave_id,leave.leave_uid,leave.leave_startTime,leave.leave_endTime,leave.leave_type,leave.leave_reason,leave.leave_state,leave_type.type_name,leave_type.type_prove,leave_prove.prove_image_path,audience.audience_name' FROM 'leave' LEFT JOIN leave_type on 'leave.leave_type = leave_type.type_id' LEFT JOIN leave_prove on 'leave.leave_id = leave_prove.leave_id' LEFT JOIN audience on 'leave.leave_uid = audience.audience_id' ORDER BY 'leave.leave_id' desc";
	//$arr_s=M('leave','','DB_MEETING')->query($sql_leave);
		//dump($arr_s);
	
	//}
	public function detail(){
		if($_POST){
			$detail = M('leave','','DB_MEETING')->join('leave_type on leave.leave_type = leave_type.type_id')->join('leave_prove on leave.leave_id = leave_prove.leave_id')->join('audience on leave.leave_uid = audience.audience_id')->where('leave.leave_id='.$_POST['leave_id'])->field('leave.leave_id,leave.leave_uid,leave.leave_startTime,leave.leave_endTime,leave.leave_type,leave.leave_reason,leave.leave_state,leave_type.type_name,leave_type.type_prove,leave_prove.prove_image_path,audience.audience_name')->find();
			$detail_log = M('leave_log','','DB_MEETING')->join('audience on audience.audience_id = leave_log.uid')->where('leave_log.leave_id='.$_POST['leave_id'])->field('leave_log.*,audience.audience_name')->select();
		}
		//dump($detail_log);
		
		foreach($detail_log as $key=>$value){
			switch($value['leave_state']){
				case 0:
					$value['leave_state'] = "审批中";
				break;
				case 1:
					$value['leave_state'] = "已审批";
				break;
				case 2:
					$value['leave_state'] = "已驳回";
				break;
				case 3:
					$value['leave_state'] = "申请销假";
				break;
				case 4:
					$value['leave_state'] = "同意销假";
				break;
				case 5:
					$value['leave_state'] = "驳回销假";
				break;
				
			}
			
			$leave_log[$value['leave_state']] = $value;
			$leave_log[$value['leave_state']]['count']=4-mb_strlen($value['leave_state'],"utf-8");
		}
			
		$detail['leave_days'] = ($detail['leave_endTime']-$detail['leave_startTime'])%(3600*24)==0?($detail['leave_endTime']-$detail['leave_startTime'])/(3600*24)."天":floor(($detail['leave_endTime']-$detail['leave_startTime'])/(3600*24))."天/".floor((($detail['leave_endTime']-$detail['leave_startTime'])%(3600*24))/3600)."小时";		
		$this->assign("detail",$detail);
		$this->assign("leave_log",$leave_log);
		$this->display();
	}
// +---------------------------------------+
// | 请假类型列表                     	   |
// +---------------------------------------+
// | last update author: zhanghao		   |
// | last update time  : 2014-11-06 15:41  |
// +---------------------------------------+	
	public function leave_type(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$page = pageing('leave_type',10,'',$_GET['seach'],'type_id desc','1','type_name','',"DB_MEETING");
		$this->assign("page",$page['data']);
		$this->assign("show",$page['show']);
		$this->display();
	}
	
	public function update_type(){
		$rs=M('leave_type','','DB_MEETING')->where('type_id='.$_GET['type_id'])->find();
		$this->assign('rs',$rs);
		$this->display();	
	}
	
	public function update_type_act(){
		
		if($_FILES['type_image']['name']!='')
		$this->imgUpload();
		$rs = M('leave_type','','DB_MEETING')->where('type_id='.$_POST['type_id'])->save($_POST);
		if($rs){
			$this->success("修改成功!",__URL__.'/leave_type');
		} else {
			$this->error("修改失败！");
		}		
	}
	
	public function imgUpload(){
		$audience_image_path = 'Public/Uploads/';
		if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);
		$audience_image_path.= 'leave_type/';
		if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);
		$audience_image_path.= $_SESSION['cid'] .'/';
		if(!is_dir($audience_image_path))mkdir($audience_image_path,0777,true);

		import('ORG.Net.UploadFile');
		$config['savePath'] = $audience_image_path;
		$config['allowExts']= array('jpg', 'gif', 'png', 'jpeg');
		$config['maxSize']= 3145728;
		$config['thumb'] = true;
		$config['thumbPrefix'] = 'm_';
		$config['thumbMaxWidth'] = '72';
		$config['thumbMaxHeight'] = '74';
		$upload = new UploadFile($config);
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['type_image']	=$audience_image_path.$uploadList[0]['savename'];
			$_POST['type_image_s']	=$audience_image_path.$config['thumbPrefix'].$uploadList[0]['savename'];
		}
	}


}


?>